package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.MarketCarGateInfoDto;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.car.viewhelper.CarStockCountListTopViewhelper;

/**
 * 库存统计下探页面controller
 * 
 * @author tianshifu
 * 
 */
public class CarStockCountDetailController extends TmsController {

	private static final String MEMID = "memId";
	private static final String SERIAL = "serial";
	private static final String CARSCORE = "carScore";


	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			// 从uc中获取商户ID
			String memId = (String) uc.getAttribute(MEMID);

			// 根据商户ID查询出商户库存车辆信息，打出一个list，在前台显示,这块需要另外几个通过list计算的参数
			// 查询所有入场时间不为空的车辆信息
			Car car = new Car();

			List<MarketCarInfoDto> list = car.getCarStockCountInfoByMemId(memId);
			Member member = Member.getInstance();
			
			String shopName = "";
			int shopCarNo = 0;
			int numCount = 0;
			int inCount = 0;
			
			List gatesList = new ArrayList();
			int i = 1;
			member = member.findMemberInfoByMemId(memId);
			if(member != null){
				shopName = member.getRealName();
				shopCarNo = member.getCarPort();
			}
			numCount = car.findCarCountByMem(2,memId);
			inCount = car.findCarCountByMem(1,memId);
			if (list.size() > 0 && list != null) {
				for (MarketCarInfoDto dto : list) {
					TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();
					transferAcceptedInfo = transferAcceptedInfo.getTransferBybasicCarId(dto.getSerial());
					dto.setTransferSerial(transferAcceptedInfo.getSerial());
					if(i%2 == 0){
						dto.setCssFlag(2);
					}else{
						dto.setCssFlag(1);
					}
					dto.setNum(i);
					if(StringUtil.isNullOrBlank(dto.getCarName())){
						dto.setCarName("-");
					}
					if(StringUtil.isNullOrBlank(DateUtil.getTimeByTimeStamp(dto.getEnterTime()))){
						dto.setEnterTimeS("-");
					}else{
						dto.setEnterTimeS(DateUtil.getTimeByTimeStamp(dto.getEnterTime()));
					}
					if(StringUtil.isNullOrBlank(DateUtil.getTimeByTimeStamp(dto.getStorageTime()))){
						dto.setStorageTimeS("-");
					}else{
						dto.setStorageTimeS(DateUtil.getTimeByTimeStamp(dto.getStorageTime()));
					}
					i++;
					gatesList.add(dto);
				}
			}
			CarStockCountListTopViewhelper view = new CarStockCountListTopViewhelper();

			view.setGatesInfoIOList(gatesList);

			// 添加页面头信息
			view.setShopName(shopName);
			view.setShopCarNo(String.valueOf(shopCarNo));
			view.setShopCount(String.valueOf(numCount));
			view.setInCount(String.valueOf(inCount));

			uc.removeAttribute(TmsConstants.VIEWHELP);
			uc.setAttribute(TmsConstants.VIEWHELP, view);// 将list打到前台
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	public void detail(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		try {
			controller.begin();

			String memId = (String) uc.getAttribute(MEMID);
			String carScore = (String) uc.getAttribute(CARSCORE);

			Car car = new Car();
			String shopName = "";
			int shopCarNo = 0;
			int inCount = 0;

			Member member = Member.getInstance();
			member = member.findMemberInfoByMemId(memId);
			if(member != null){
				shopName = member.getRealName();
				shopCarNo = member.getCarPort();
			}
			inCount = car.findCarCountByMem(1,memId);
			
			List<MarketCarInfoDto> cars = car.getCarInfo(memId, carScore);
			if (cars.size() > 0 && cars != null) {
				for (int i = 0; i < cars.size(); i++) {
					MarketCarInfoDto dto = cars.get(i);
					dto.setNum(i+1);
					dto.setCssFlag(i%2 == 0 ? 2 : 1);
					if(StringUtil.isNullOrBlank(dto.getCarName())){
						dto.setCarName("-");
					}
					if(StringUtil.isNullOrBlank(DateUtil.getTimeByTimeStamp(dto.getEnterTime()))){
						dto.setEnterTimeS("-");
					}else{
						dto.setEnterTimeS(DateUtil.getTimeByTimeStamp(dto.getEnterTime()));
					}
					if(StringUtil.isNullOrBlank(DateUtil.getTimeByTimeStamp(dto.getStorageTime()))){
						dto.setStorageTimeS("-");
					}else{
						dto.setStorageTimeS(DateUtil.getTimeByTimeStamp(dto.getStorageTime()));
					}
				}
			}
			
			final CarStockCountListTopViewhelper view = new CarStockCountListTopViewhelper();
			view.setMarketCarInfos(cars);
			// 添加页面头信息
			view.setMemId(memId);
			view.setCarScore(carScore);
			view.setShopName(shopName);
			view.setShopCarNo(String.valueOf(shopCarNo));
			view.setInCount(String.valueOf(inCount));
			uc.removeAttribute(TmsConstants.VIEWHELP);
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}
	
	public void record(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		try {
			controller.begin();

			Car car = new Car();
			String serial = (String) uc.getAttribute(SERIAL);
			List<MarketCarGateInfoDto> records = car.findInoutResult(serial);
			if (records.size() > 0 && records != null) {
				for (int i = 0; i < records.size(); i++) {
					MarketCarGateInfoDto dto = records.get(i);
					dto.setNum(i+1);
					dto.setCssFlag(i%2 == 0 ? 2 : 1);
					if (StringUtil.isNullOrBlank(DateUtil.getTimeByTimeStamp(dto.getInsertTime()))) {
						dto.setInsertTimeS("-");
					} else {
						dto.setInsertTimeS(DateUtil.getTimeByTimeStamp(dto.getInsertTime()));
					}
				}
			}
			
			final CarStockCountListTopViewhelper view = new CarStockCountListTopViewhelper();
			view.setGateInfos(records);
			// 添加页面头信息
			MarketCarGateInfoDto info = records != null ? records.get(0) : null;
			view.setLicensePlate(info != null ? info.getLicensePlate() : "-");
			view.setMemId((String)uc.getAttribute(MEMID));
			view.setCarScore((String)uc.getAttribute(CARSCORE)); 
			uc.removeAttribute(TmsConstants.VIEWHELP);
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}
}
